package com.experienceBaby.Cashbook.fragment;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.experienceBaby.Cashbook.R;
import com.experienceBaby.Cashbook.adapter.MyListviewAdapter;
import com.experienceBaby.Cashbook.bean.Ranking;
import com.experienceBaby.Cashbook.databinding.FragmentChartBinding;
import com.experienceBaby.Cashbook.databinding.FragmentChartSmallBinding;
import com.experienceBaby.Cashbook.greendao.DbController;
import com.experienceBaby.Cashbook.view.MyMarkerView;
import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.components.Description;
import com.github.mikephil.charting.components.LimitLine;
import com.github.mikephil.charting.components.MarkerView;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.List;

public class ChartSmallFragment extends Fragment {
    LineChart lc;
    FragmentChartSmallBinding binding;
    int year, month = 0;
    boolean aboolean;
    ListView fcs_lv;
    DbController dbController;

    ChartSmallFragment(int year, int month, boolean aboolean) {
        this.year = year;
        this.month = month;
        this.aboolean = aboolean;
    }


    @Nullable
    @org.jetbrains.annotations.Nullable
    @Override
    public View onCreateView(@NonNull @NotNull LayoutInflater inflater, @Nullable @org.jetbrains.annotations.Nullable ViewGroup container, @Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        binding = FragmentChartSmallBinding.inflate(inflater, container, false);

        initView_MONTH();


        return binding.getRoot();
    }


    @SuppressLint("DefaultLocale")
    private void initView_MONTH() {
        String lotalCost;
        String avgCost;
        lc = binding.lc;
        dbController = new DbController(getContext());
        List<Double> costs;
        Ranking ranking;
        if (month == 0) {
            ranking = dbController.searchByYearGroupByType(year, aboolean);
        } else {
            ranking = dbController.searchByMonthGroupByType(year, month, aboolean);
        }
        costs = ranking.getCosts();
        lotalCost = String.format("%.2f", ranking.getLotalCost());
        avgCost = String.format("%.2f", ranking.getAvgCost());
        for (int i = 0; i < costs.size(); i++) {
            if (costs.get(i) == null) {
                costs.set(i, Double.valueOf(0));
            }
        }

        lc.setDragEnabled(false);// 是否可以拖拽
        lc.setScaleEnabled(false);// 是否可以缩放 x和y轴, 默认是true
        lc.setScaleXEnabled(false); //是否可以缩放 仅x轴
        lc.setScaleYEnabled(false); //是否可以缩放 仅y轴
        lc.setPinchZoom(false);  //设置x轴和y轴能否同时缩放。默认是否
        lc.setDoubleTapToZoomEnabled(false);//设置是否可以通过双击屏幕放大图表。默认是true

        XAxis xAxis = lc.getXAxis();
        xAxis.setLabelCount(costs.size());
        if (month == 0) {
            xAxis.setGranularity(1f);
        } else {
            xAxis.setGranularity(5f);
        }
        xAxis.setAxisMinimum(1);
        xAxis.setAxisMaximum(costs.size()-1);
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
        xAxis.setGridColor(android.R.color.transparent);

        Double max1;
        max1 = Collections.max(costs);
        String max = String.format("%.2f", max1);
        LimitLine limitLine = new LimitLine(Float.parseFloat(max), max);

        YAxis yAxisr = lc.getAxisRight();
        yAxisr.setEnabled(false);
        YAxis yAxisl = lc.getAxisLeft();
        yAxisl.setTextColor(android.R.color.transparent);
        yAxisl.setGridColor(android.R.color.transparent);
        yAxisl.setAxisLineColor(android.R.color.transparent);
        yAxisl.setAxisMinimum(0);
        yAxisl.addLimitLine(limitLine);


        Description description = lc.getDescription();
        description.setText("总支出：" + lotalCost + "    平均值：" + avgCost);
        description.setTextSize(10f);
        description.setPosition(560, 50);

        lc.getLegend().setEnabled(false);

        MyMarkerView mv = new MyMarkerView(getContext(), R.layout.marker_view);
        mv.setChartView(lc);
        lc.setMarker(mv);


        List<Entry> list = new ArrayList<>();
        for (int i = 0; i < costs.size(); i++) {
            String s = String.format("%.2f", costs.get(i));
            float f = Float.parseFloat(s);
            list.add(new Entry(i, f));
        }
        LineDataSet dataSet = new LineDataSet(list, "");
        dataSet.setValueTextColor(android.R.color.transparent);
        dataSet.setColor(R.color.purple_700);
        dataSet.setCircleColor(R.color.purple_700);
        LineData lineData = new LineData(dataSet);
        lc.setData(lineData);
        lc.invalidate();

        MyListviewAdapter myListviewAdapter;
        if (month == 0) {
            myListviewAdapter = new MyListviewAdapter(getContext(), dbController.
                    searchByYearGroupByType(year, false).getDatas());
        } else {
            myListviewAdapter = new MyListviewAdapter(getContext(), dbController.
                    searchByMonthGroupByType(year, month, aboolean).getDatas());
        }
        fcs_lv = binding.fcsLv;
        fcs_lv.setAdapter(myListviewAdapter);

    }
}
